home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 11 object serialization / xmlserializationdemo / module1.vb < prev    next >
Text File  |  2002-03-19  |  18KB  |  385 lines

  1. Imports System.IO
  2. Imports System.Xml.Serialization
  3.  
  4. Module MainModule
  5.  
  6.     Sub Main()
  7.         ' Run one of the Textxxxx procedures below by uncommenting only one statement
  8.  
  9.         'TestXmlSerializer()
  10.         'TestXmlSerializer2()
  11.         'TestXmlSerializer3()
  12.         'TestXmlSerializer4()
  13.         'TestDeserializationEvents()
  14.         'TestSerializationOverriding()
  15.         'TestSerializationOverriding2()
  16.  
  17.         ' These statements are usuful when running inside Visual Studio.NET
  18.         Console.WriteLine("")
  19.         Console.WriteLine(">>> Press Enter to terminate the program <<<")
  20.         Console.ReadLine()
  21.  
  22.     End Sub
  23.  
  24.     ' this procedure tests the XmlSerializer
  25.  
  26.     Sub TestXmlSerializer()
  27.         ' Create an XMLSerializer object for the Customer class.
  28.         Dim ser As New XmlSerializer(GetType(Customer))
  29.         ' Create a Publisher object.
  30.         Dim cust As New Customer(1, "Joe Doe", Nothing, "New York")
  31.  
  32.         ' Open the destination file.
  33.         Dim fs As New FileStream("c:\customer.xml", FileMode.Create)
  34.         ' Serialize the object to the stream and close it
  35.         ser.Serialize(fs, cust)
  36.         fs.Close()
  37.  
  38.         ' Reopen the stream.
  39.         Dim fs2 As New FileStream("c:\customer.xml", FileMode.Open)
  40.         ' Deserialize the file into another Customer object, and close the stream.
  41.         Dim cust2 As Customer = CType(ser.Deserialize(fs2), Customer)
  42.         fs2.Close()
  43.         ' check object properties
  44.         Console.WriteLine(cust2.Name & ", " & cust2.City)   ' => Joe Doe, New York
  45.     End Sub
  46.  
  47.     ' this procedure tests the Publisher2 class, which contains attributes
  48.  
  49.     Sub TestXmlSerializer2()
  50.         ' Create an XMLSerializer object for the Customer class.
  51.         Dim ser As New XmlSerializer(GetType(Customer2))
  52.         ' Create a Publisher object.
  53.         Dim cust As New Customer2(1, "Joe Doe", Nothing, "New York")
  54.  
  55.         ' Open the destination file.
  56.         Dim fs As New FileStream("c:\customer.xml", FileMode.Create)
  57.         ' Serialize the object to the stream and close it
  58.         ser.Serialize(fs, cust)
  59.         fs.Close()
  60.  
  61.         ' Reopen the stream.
  62.         Dim fs2 As New FileStream("c:\customer.xml", FileMode.Open)
  63.         ' Deserialize the file into another Customer object, and close the stream.
  64.         Dim cust2 As Customer2 = CType(ser.Deserialize(fs2), Customer2)
  65.         fs2.Close()
  66.         ' check object properties
  67.         Console.WriteLine(cust2.Name & ", " & cust2.City)   ' => Joe Doe, New York
  68.     End Sub
  69.  
  70.     ' this procedure tests the serialization of nested objects.
  71.  
  72.     Sub TestXmlSerializer3()
  73.         ' Create an XMLSerializer object for the Customer class.
  74.         Dim ser As New XmlSerializer(GetType(Customer4))
  75.         ' Create a Publisher object.
  76.         Dim cust As New Customer4(1, "Joe Doe", Nothing, "New York")
  77.         ' add some orders
  78.         cust.Orders(0) = New Order(1, #1/2/2001#, 123.5)
  79.         cust.Orders(1) = New Order(2, #4/8/2001#, 450.8)
  80.  
  81.         ' Open the destination file.
  82.         Dim fs As New FileStream("c:\customer.xml", FileMode.Create)
  83.         ' Serialize the object to the stream and close it
  84.         ser.Serialize(fs, cust)
  85.         fs.Close()
  86.  
  87.         ' Reopen the stream.
  88.         Dim fs2 As New FileStream("c:\customer.xml", FileMode.Open)
  89.         ' Deserialize the file into another Customer object, and close the stream.
  90.         Dim cust2 As Customer4 = CType(ser.Deserialize(fs2), Customer4)
  91.         fs2.Close()
  92.  
  93.         ' check object properties
  94.         Console.WriteLine(cust2.Name & ", " & cust2.City)   ' => Joe Doe, New York
  95.         Console.WriteLine(cust2.Orders(0).Date)             ' => 1/2/2001 12:00:00 AM
  96.     End Sub
  97.  
  98.     ' this procedure tests serialization of nested objects with namespaces
  99.  
  100.     Sub TestXmlSerializer4()
  101.         ' Create an XMLSerializer object for the Customer class.
  102.         Dim ser As New XmlSerializer(GetType(Customer4))
  103.         ' Create a Publisher object.
  104.         Dim cust As New Customer4(1, "Joe Doe", Nothing, "New York")
  105.         ' add some orders
  106.         cust.Orders(0) = New Order(1, #1/2/2001#, 123.5)
  107.         cust.Orders(1) = New Order(2, #4/8/2001#, 450.8)
  108.  
  109.         Dim ns As New XmlSerializerNamespaces()
  110.         ns.Add("Customer4", "http://www.vb2themax.com")
  111.         ns.Add("Order", "http://www.wintellect.com")
  112.  
  113.         ' Open the destination file.
  114.         Dim fs As New FileStream("c:\customer.xml", FileMode.Create)
  115.         ' Serialize the object to the stream, enforcing the specified namespaces.
  116.         ser.Serialize(fs, cust, ns)
  117.         fs.Close()
  118.  
  119.         ' Reopen the stream, using the same XmlSerializer.
  120.         Dim fs2 As New FileStream("c:\customer.xml", FileMode.Open)
  121.         ' Deserialize the file into another Customer object, and close the stream.
  122.         Dim cust2 As Customer4 = CType(ser.Deserialize(fs2), Customer4)
  123.         fs2.Close()
  124.  
  125.         ' check object properties
  126.         Console.WriteLine(cust2.Name & ", " & cust2.City)   ' => Joe Doe, New York
  127.         Console.WriteLine(cust2.Orders(0).Date)             ' => 1/2/2001 12:00:00 AM
  128.     End Sub
  129.  
  130.     ' this procedure tests deserialization events.
  131.  
  132.     Sub TestDeserializationEvents()
  133.         ' Create an XMLSerializer object for the Publisher class.
  134.         Dim ser As New XmlSerializer(GetType(Customer))
  135.         ' dynamically create the event handler
  136.         AddHandler ser.UnknownElement, AddressOf Deserialization_UnknownElement
  137.  
  138.         ' Reopen the stream.
  139.  
  140.         Dim fs2 As New FileStream("c:\customer2.xml", FileMode.Open)
  141.         ' Deserialize the file into another Customer object, and close the stream.
  142.         Dim cust2 As Customer = CType(ser.Deserialize(fs2), Customer)
  143.         fs2.Close()
  144.  
  145.         ' check object properties
  146.         Console.WriteLine(cust2.Name & ", " & cust2.City)   ' => Joe Doe, New York
  147.     End Sub
  148.  
  149.     ' this event is raised when the XMLSerializer founds an unknown XML element
  150.  
  151.     Sub Deserialization_UnknownElement(ByVal sender As Object, ByVal e As XmlElementEventArgs)
  152.         ' cast the element to a Customer object
  153.         Dim cust As Customer = CType(e.ObjectBeingDeserialized, Customer)
  154.         ' there are two cases: we've found a FirstName or LastName XML element
  155.         If e.Element.Name = "FirstName" Then
  156.             ' if it is a FirstName, prefix the current Name property.
  157.             cust.Name = e.Element.InnerXml
  158.         ElseIf e.Element.Name = "LastName" Then
  159.             ' if it is a Lastname element, append to current Name property.
  160.             cust.Name += " " & e.Element.InnerXml
  161.         End If
  162.     End Sub
  163.  
  164.     ' this procedure tests XML serialization overriding
  165.  
  166.     Sub TestSerializationOverriding()
  167.         ' step 1: create an XmlAttributeOverrides object
  168.         Dim xmlAttrOver As New XmlAttributeOverrides()
  169.  
  170.         ' (A) add the XmlRootAttribute("EmployeeList") attribute
  171.  
  172.         ' step A2: create the XmlAttributes object.
  173.         Dim xmlAttrs As New XmlAttributes()
  174.         ' step A3: create and initialize the XmlRootAttribute
  175.         Dim attr1 As New XmlRootAttribute("EmployeeList")
  176.         ' step A4: assign it to the correct property of the XmlAttributes object
  177.         xmlAttrs.XmlRoot = attr1
  178.         ' step A5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides 
  179.         ' When you add an XmlRootAttribute you specify only 2 arguments:
  180.         ' the 1st argument is the class that contains the overridden member
  181.         ' the 2nd argument is the XmlAttributes object that defines how the member is overridden.
  182.         xmlAttrOver.Add(GetType(AddressBook), xmlAttrs)
  183.  
  184.         ' (B) add the XmlArray("Employees") and XmlArrayItem("Employee") attributes.
  185.  
  186.         ' step B2: create a new XmlAttributes object.
  187.         xmlAttrs = New XmlAttributes()
  188.  
  189.         ' step B3: create and initialize the XmlArrayAttribute object.
  190.         Dim attr2 As New XmlArrayAttribute("Employees")
  191.         ' step B4: assign it to the correct property of the XmlAttributes object
  192.         xmlAttrs.XmlArray = attr2
  193.         ' again step B3/B4, this time for the XmlArrayItemAttribute object.
  194.         ' (Note that you can do both creation and assignment in one step.)
  195.         xmlAttrs.XmlArrayItems.Add(New XmlArrayItemAttribute("Employee"))
  196.         ' step B5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides 
  197.         ' the 1st argument is the class that contains the overridden member
  198.         ' the 2nd argument is the member being overridden
  199.         ' the 3rd argument is the XmlAttributes object that defines how the member is overridden.
  200.         xmlAttrOver.Add(GetType(AddressBook), "Contacts", xmlAttrs)
  201.  
  202.         ' (C) add the XmlAttributeAttribute("ID") attribute
  203.  
  204.         ' step C2: create a new XmlAttributes object.
  205.         xmlAttrs = New XmlAttributes()
  206.         ' step C3/C4: create and initialize the XmlArrayAttribute object
  207.         ' and assign it to the 
  208.         xmlAttrs.XmlAttribute = New XmlAttributeAttribute("id")
  209.         ' step C5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides 
  210.         ' (arguments are as in step B5)
  211.         xmlAttrOver.Add(GetType(Person), "ID", xmlAttrs)
  212.  
  213.         ' (D) add the XmlElement("Phone") attribute
  214.  
  215.         ' step D2: create a new XmlAttributes object.
  216.         xmlAttrs = New XmlAttributes()
  217.         ' step D3/D4: create and initialize the XmlElementAttribute object
  218.         ' and add it to the XmlAttributes's XmlElements collection
  219.         xmlAttrs.XmlElements.Add(New XmlElementAttribute("Phone"))
  220.         ' step D5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides 
  221.         ' (arguments are as in step B5)
  222.         xmlAttrOver.Add(GetType(Person), "PhoneNumber", xmlAttrs)
  223.  
  224.         ' Step 6: create the XmlSerializer that eses the XmlAttributeOverrides.
  225.         Dim ser As New XmlSerializer(GetType(AddressBook), xmlAttrOver)
  226.  
  227.         '----  create an object tree.
  228.         Dim addrBook As New AddressBook()
  229.         addrBook.Name = "My Employee Book"     ' Name of this address book
  230.         ReDim addrBook.Contacts(1)          ' We expect two Persons.
  231.  
  232.         Dim p0 As New Person()              ' First Person
  233.         p0.ID = 1
  234.         p0.Name = "Joe Doe"
  235.         p0.PhoneNumber = "234-456-6789"
  236.         Dim p1 As New Person()              ' Second Person
  237.         p1.ID = 2
  238.         p1.Name = "Robert Smith"
  239.         p1.PhoneNumber = "234-987-6543"
  240.         addrBook.Contacts(0) = p0           ' Store into the Contacts array.
  241.         addrBook.Contacts(1) = p1
  242.  
  243.         ' Open the destination file.
  244.         Dim fs As New FileStream("c:\addrbook.xml", FileMode.Create)
  245.         ' Serialize the object to the stream and close it
  246.         ser.Serialize(fs, addrBook)
  247.         fs.Close()
  248.  
  249.         ' Reopen the stream.
  250.         Dim fs2 As New FileStream("c:\addrbook.xml", FileMode.Open)
  251.         ' Deserialize the file into another Customer object, and close the stream.
  252.         Dim addrBook2 As AddressBook = CType(ser.Deserialize(fs2), AddressBook)
  253.         fs2.Close()
  254.  
  255.         ' check object properties
  256.         Dim p As Person = CType(addrBook.Contacts(0), Person)
  257.         Console.WriteLine(p.ID.ToString & " - " & p.Name & " " & p.PhoneNumber)
  258.         p = CType(addrBook.Contacts(1), Person)
  259.         Console.WriteLine(p.ID.ToString & " - " & p.Name & " " & p.PhoneNumber)
  260.  
  261.     End Sub
  262.  
  263.     ' this procedure tests xmlserializer overriding with inherited classes
  264.  
  265.     Sub TestSerializationOverriding2()
  266.         ' step 1: create an XmlAttributeOverrides object
  267.         Dim xmlAttrOver As New XmlAttributeOverrides()
  268.  
  269.         ' (A) add the XmlRootAttribute("EmployeeList") attribute
  270.  
  271.         ' step A2: create the XmlAttributes object.
  272.         Dim xmlAttrs As New XmlAttributes()
  273.         ' step A3: create and initialize the XmlRootAttribute
  274.         Dim attr1 As New XmlRootAttribute("EmployeeList")
  275.         ' step A4: assign it to the correct property of the XmlAttributes object
  276.         xmlAttrs.XmlRoot = attr1
  277.         ' step A5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides 
  278.         ' When you add an XmlRootAttribute you specify only 2 arguments:
  279.         ' the 1st argument is the class that contains the overridden member
  280.         ' the 2nd argument is the XmlAttributes object that defines how the member is overridden.
  281.         xmlAttrOver.Add(GetType(AddressBook), xmlAttrs)
  282.  
  283.         ' (B) add the XmlArray("Employees") and XmlArrayItem("Employee") attributes.
  284.  
  285.         ' step B2: create a new XmlAttributes object.
  286.         xmlAttrs = New XmlAttributes()
  287.  
  288.         ' step B3/B4: create and initialize the XmlArrayAttribute object.
  289.         '  and assign it to the correct property of the XmlAttributes object
  290.         xmlAttrs.XmlArray = New XmlArrayAttribute("Employees")
  291.         ' again step B3/B4, this time for the two XmlArrayItemAttribute objects.
  292.         xmlAttrs.XmlArrayItems.Add(New XmlArrayItemAttribute("Employee", GetType(Employee)))
  293.         xmlAttrs.XmlArrayItems.Add(New XmlArrayItemAttribute("CandidateEmployee", GetType(CandidateEmployee)))
  294.  
  295.         ' step B5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides 
  296.         ' the 1st argument is the class that contains the overridden member
  297.         ' the 2nd argument is the member being overridden
  298.         ' the 3rd argument is the XmlAttributes object that defines how the member is overridden.
  299.         xmlAttrOver.Add(GetType(AddressBook), "Contacts", xmlAttrs)
  300.  
  301.         ' (C) add the XmlAttributeAttribute("ID") attribute
  302.  
  303.         ' step C2: create a new XmlAttributes object.
  304.         xmlAttrs = New XmlAttributes()
  305.         ' step C3/C4: create and initialize the XmlAttribute object
  306.         ' and assign it to the correct property of XmlAttributes object
  307.         xmlAttrs.XmlAttribute = New XmlAttributeAttribute("id")
  308.         ' step C5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides 
  309.         ' (arguments are as in step B5)
  310.         xmlAttrOver.Add(GetType(Person), "ID", xmlAttrs)
  311.  
  312.         ' (D) add the XmlElement("Phone") attribute
  313.  
  314.         ' step D2: create a new XmlAttributes object.
  315.         xmlAttrs = New XmlAttributes()
  316.         ' step D3/D4: create and initialize the XmlElementAttribute object
  317.         ' and add it to the XmlAttributes's XmlElements collection
  318.         xmlAttrs.XmlElements.Add(New XmlElementAttribute("Phone"))
  319.         ' step D5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides 
  320.         ' (arguments are as in step B5)
  321.         xmlAttrOver.Add(GetType(Person), "PhoneNumber", xmlAttrs)
  322.  
  323.         ' (E) add the XmlAttribute() attribute to the SSN element in Employee class
  324.  
  325.         ' step E2: create a new XmlAttributes object.
  326.         xmlAttrs = New XmlAttributes()
  327.         ' step E3/E4: create and initialize the XmlAttribute object
  328.         ' and assign it to the correct property of XmlAttributes object
  329.         xmlAttrs.XmlAttribute = New XmlAttributeAttribute("SSN")
  330.         ' step E5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides 
  331.         xmlAttrOver.Add(GetType(Employee), "SSN", xmlAttrs)
  332.  
  333.         ' (F) add the XmlIgnore() attribute to the HireDate element in Employee class
  334.  
  335.         ' step F2: create a new XmlAttributes object.
  336.         xmlAttrs = New XmlAttributes()
  337.         ' step F3/F4: note that you don't actually create an XmlIgnoreAttribute object;
  338.         ' as for all those attribute whose presence is enough to affect the serialization behavior,
  339.         ' in this case you just set the corresponding XmlIgnore property to True.
  340.         xmlAttrs.XmlIgnore = True
  341.         ' step F5: pass the XmlAttributes object to the Add method of XmlAttributeOverrides 
  342.         xmlAttrOver.Add(GetType(Employee), "HireDate", xmlAttrs)
  343.  
  344.         ' Step 6: create the XmlSerializer that eses the XmlAttributeOverrides.
  345.         Dim ser As New XmlSerializer(GetType(AddressBook), xmlAttrOver)
  346.  
  347.         '----  create an object tree.
  348.         Dim addrBook As New AddressBook()
  349.         addrBook.Name = "My Address Book"     ' Name of this address book
  350.         ReDim addrBook.Contacts(1)          ' We expect two Employee.
  351.  
  352.         Dim e0 As New Employee()                    ' First, an Employee
  353.         e0.Name = "Joe Doe"
  354.         e0.SSN = "111-222-3333"
  355.         e0.PhoneNumber = "234-456-6789"
  356.         Dim e1 As New CandidateEmployee()            ' Second, a CandidateEmployee
  357.         e1.ID = 2
  358.         e1.Name = "Robert Smith"
  359.         e1.PhoneNumber = "234-987-6543"
  360.         e1.InterviewDate = #2/8/2001#
  361.         addrBook.Contacts(0) = e0           ' Store into the Contacts array.
  362.         addrBook.Contacts(1) = e1
  363.  
  364.         ' Open the destination file.
  365.         Dim fs As New FileStream("c:\addrbook.xml", FileMode.Create)
  366.         ' Serialize the object to the stream and close it
  367.         ser.Serialize(fs, addrBook)
  368.         fs.Close()
  369.  
  370.         ' Reopen the stream.
  371.         Dim fs2 As New FileStream("c:\addrbook.xml", FileMode.Open)
  372.         ' Deserialize the file into another Customer object, and close the stream.
  373.         Dim addrBook2 As AddressBook = CType(ser.Deserialize(fs2), AddressBook)
  374.         fs2.Close()
  375.  
  376.         ' check object properties
  377.         Dim e As Employee = CType(addrBook.Contacts(0), Employee)
  378.         Console.WriteLine(e.Name & " " & e.PhoneNumber & " " & e.SSN)
  379.         Dim ce As CandidateEmployee = CType(addrBook.Contacts(1), CandidateEmployee)
  380.         Console.WriteLine(ce.Name & " " & ce.PhoneNumber & " " & ce.InterviewDate)
  381.  
  382.     End Sub
  383.  
  384. End Module
  385.